package com.team622.shop.goods.service.controller;

import com.team622.shop.common.PageResult;
import com.team622.shop.common.Result;
import com.team622.shop.goods.model.entity.Goods;
import com.team622.shop.goods.model.vo.GoodsDetailsVO;
import com.team622.shop.goods.model.vo.GoodsListDetailsVO;
import com.team622.shop.goods.model.vo.GoodsPageVO;
import com.team622.shop.goods.service.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  商品前端控制器
 * </p>
 *
 * @author team622
 * @since 2025-07-16
 */
@Tag(name = "商品管理", description = "商品相关接口")
@Slf4j
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 添加商品
     * @param goods 商品信息
     * @return 添加结果
     */
    @Operation(summary = "添加商品", description = "添加新的商品信息")
    @ApiResponse(responseCode = "200", description = "添加成功")
    @PostMapping("")
    public Result<Object> add(@RequestBody Goods goods) {
        log.info("添加商品：{}", goods);
        goodsService.save(goods);
        return Result.success();
    }

    /**
     * 根据ID查询商品
     * @param id 商品ID
     * @return 商品信息
     */
    @Operation(summary = "根据ID查询商品", description = "根据商品ID查询详细信息")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/{id}")
    public Result<Goods> findById(@Parameter(description = "商品ID") @PathVariable("id") long id) {
        log.info("根据ID查询商品：{}", id);
        Goods goods = goodsService.getById(id);
        return Result.success(goods);
    }



    @Operation(summary = "根据ID查询商品详情", description = "根据商品ID查询详细信息")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("findDetailsById/{id}")
    public Result<GoodsDetailsVO> findDetailsById(@Parameter(description = "商品ID") @PathVariable("id") long id) {
        log.info("根据ID查询商品：{}", id);
        GoodsDetailsVO goodsDetailsVO = goodsService.findDetailsById(id);
        return Result.success(goodsDetailsVO);
    }


    @Operation(summary = "根据ID列表查询商品详情")
    @PostMapping("findGoodsListDetailByIds")
    public Result<List<GoodsListDetailsVO>> findGoodsListDetailByIds(@RequestBody List<Long> ids) {
        log.info("根据ID查询商品：{}", ids);
        return Result.success(goodsService.findGoodsListDetailByIds(ids));
    }

    /**
     * 分页查询商品列表
     * @param current 页码
     * @param pageSize 每页数量
     * @param name 商品名称(模糊查询)
     * @param status 状态 0:下架 1:上架
     * @return 分页结果
     */
    @Operation(summary = "分页查询商品列表", description = "根据条件分页查询商品列表")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/page")
    public PageResult<GoodsPageVO> page(
            @Parameter(description = "页码") @RequestParam(value = "current", defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @Parameter(description = "商品名称(模糊查询)") @RequestParam(value = "name", required = false) String name,
            @Parameter(description = "商品分类ID") @RequestParam(value = "categoryId", required = false) Long categoryId,
            @Parameter(description = "状态 0:下架 1:上架") @RequestParam(value = "status", required = false) Byte status) {
        log.info("分页查询商品列表：current={}, pageSize={}, name={}, status={}", current, pageSize, name, status);
        return PageResult.success(goodsService.pageQuery(current,pageSize, name,categoryId, status)) ;
    }

    /**
     * 更新商品
     * @param goods 商品信息
     * @return 更新结果
     */
    @Operation(summary = "更新商品", description = "更新已有的商品信息")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody Goods goods) {
        log.info("更新商品：{}", goods);
        boolean result = goodsService.updateById(goods);
        return Result.success(result);
    }

    /**
     * 删除商品
     * @param id 商品ID
     * @return 删除结果
     */
    @Operation(summary = "删除商品", description = "根据ID删除商品")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "商品ID") @PathVariable("id") Long id) {
        log.info("删除商品：{}", id);
        boolean result = goodsService.removeById(id);
        return Result.success(result);
    }
}
